﻿using GeneralLibs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BasicForms;
using Lists;

namespace ReportGenerators.CongNo.BangDanhGiaTinhHinhThanhToan
{
    public partial class FrmBangDanhGiaTinhHinhCongNo : BasicForms.FrmSupportPrintableComponents
    {
        public FrmBangDanhGiaTinhHinhCongNo()
        {
            InitializeComponent();
        }
        DsBangDanhGiaTinhHinhThanhToanCongNo _DsSource = new DsBangDanhGiaTinhHinhThanhToanCongNo();
        int _GioiHanSoNgay = 1000;

        private void FrmBangDanhGiaTinhHinhCongNo_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            try
            {
                cmbKyBaoCao.SelectedIndex = 19;

                #region cmbTaiKhoan
                DataView dvTaiKhoan = DanhMucTaiKhoan.GetByLoaiTaiKhoan(LoaiTaiKhoan.CongNo).DataView;
                cmbTaiKhoanNoPhaiThu.DataSource = dvTaiKhoan;
                cmbTaiKhoanNoPhaiThu.DisplayColumn = "Ten";
                cmbTaiKhoanNoPhaiThu.ValueColumn = "ID";
                cmbTaiKhoanNoPhaiThu.AddVisibleColumn("SoHieu", "Số hiệu");
                cmbTaiKhoanNoPhaiThu.AddVisibleColumn("Ten", "Tên");
                cmbTaiKhoanNoPhaiThu.EditValue = DanhMucCauHinhTaiKhoanMacDinh.Instance.GetTaiKhoanIDByTinhChat("PhaiThuKhachHang");

                cmbTaiKhoanNoPhaiTra.DataSource = dvTaiKhoan;
                cmbTaiKhoanNoPhaiTra.DisplayColumn = "Ten";
                cmbTaiKhoanNoPhaiTra.ValueColumn = "ID";
                cmbTaiKhoanNoPhaiTra.AddVisibleColumn("SoHieu", "Số hiệu");
                cmbTaiKhoanNoPhaiTra.AddVisibleColumn("Ten", "Tên");
                cmbTaiKhoanNoPhaiTra.EditValue = DanhMucCauHinhTaiKhoanMacDinh.Instance.GetTaiKhoanIDByTinhChat("PhaiTraNhaCungCap");
                #endregion

                if (nmrSoNgayConLai.Text != "")
                    _GioiHanSoNgay = Convert.ToInt32(nmrSoNgayConLai.Text);

                dsTongHopCongNoKetHop.DoiTuongNghiepVu.Merge(DanhMucDoiTuongNghiepVu.GetDanhMucDoiTac().DataView.ToTable());
                gridControl.DataSource = _DsSource.BangDanhGiaThanhToan;
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }

            foreach (DevExpress.XtraGrid.Columns.GridColumn column in gridView.Columns)
            {
                column.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
                column.OptionsColumn.AllowMerge = DevExpress.Utils.DefaultBoolean.False;
            }
            FormatNumberInGridView(gridView);
        }

        private void cmbKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                FrmThamSoBaoCao.ThietLapKhoangThoiGianBaoCao(cmbKyBaoCao.Text, cmbKyBaoCao, dtNgayBatDau, dtNgayKetThuc);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            //colPhaiThu.Visible = colNgayPhaiThu.Visible = chkForBan.Checked;
            //colPhaiTra.Visible = coLNgayPhaiTra.Visible = chkForMua.Checked;

            BasicForms.FrmProgressBar.StartShowProgress();
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand(); cmd.CommandTimeout = Config.TimeOut;
                Guid taiKhoanPhaiThuID = (Guid)cmbTaiKhoanNoPhaiThu.EditValue;
                Guid taiKhoanPhaiTraID = (Guid)cmbTaiKhoanNoPhaiTra.EditValue;
                if(nmrSoNgayConLai.Text != "")
                    _GioiHanSoNgay = Convert.ToInt32(nmrSoNgayConLai.Text);

                _DsSource.BangDanhGiaThanhToan.Clear();

                if (chkForBan.Checked && chkForMua.Checked)
                {
                    cmd.CommandText = string.Format("SELECT A.So SoHoaDon, B.So SoPhieu, A.Ngay, E.Ten, E.CongNoPhaiThuMax SoDuNoToiDa,  \n" +
                                        "	A.SoTienNT PhaiThu, DATEADD(Day, A.SoNgayThanhToan, A.Ngay) NgayPhaiThu, 0 PhaiTra, NULL NgayPhaiTra, \n" +
                                        "    DATEDIFF(DAY, GETDATE(),  DATEADD(Day, A.SoNgayThanhToan, A.Ngay)) SoNgayConLai \n" +
                                        "FROM ChungTu A INNER JOIN ChungTu B ON A.ChungTuID = B.ChungTuGocID \n" +
                                        "										AND @ThoiDiemDau <= A.Ngay AND A.Ngay <= @ThoiDiemCuoi AND A.GhiSo = 1  \n" +
                                        "	INNER JOIN KhoanMucChungTu C ON C.ChungTuID = B.ChungTuID \n" +
                                        "	INNER JOIN LoaiChungTu D ON D.ID = A.LoaiChungTuID AND D.Ma IN ('HoaDonBanHang') \n" +
                                        "	INNER JOIN DoiTuongNghiepVu E ON E.ID = A.DoiTuongNghiepVu1ID \n" +
                                        "WHERE A.SoNgayThanhToan IS NOT NULL AND A.SoNgayThanhToan > 0 AND \n" +
                                        "   (A.DaThanhToan IS NULL OR A.DaThanhToan = 0)  AND  \n" +
                                        "   DATEDIFF(DAY, GETDATE(),  DATEADD(Day, A.SoNgayThanhToan, A.Ngay)) <= {0} \n" +
                                        "UNION  \n" +
                                        "SELECT A.So SoHoaDon, B.So SoPhieu, A.Ngay, E.Ten, E.CongNoPhaiThuMax SoDuNoToiDa,  \n" +
                                        "	0, NULL, A.SoTienNT PhaiTra, DATEADD(Day, A.SoNgayThanhToan, A.Ngay) NgayPhaiTra,  \n" +
                                        "    DATEDIFF(DAY, GETDATE(),  DATEADD(Day, A.SoNgayThanhToan, A.Ngay)) SoNgayConLai \n" +
                                        "FROM ChungTu A INNER JOIN ChungTu B ON A.ChungTuID = B.ChungTuGocID \n" +
                                        "										AND @ThoiDiemDau <= A.Ngay AND A.Ngay <= @ThoiDiemCuoi AND A.GhiSo = 1  \n" +
                                        "	INNER JOIN KhoanMucChungTu C ON C.ChungTuID = B.ChungTuID \n" +
                                        "	INNER JOIN LoaiChungTu D ON D.ID = A.LoaiChungTuID AND D.Ma IN ('HoaDonMuaHang') \n" +
                                        "	INNER JOIN DoiTuongNghiepVu E ON E.ID = A.DoiTuongNghiepVu1ID \n" +
                                        "WHERE A.SoNgayThanhToan IS NOT NULL AND A.SoNgayThanhToan > 0 AND \n" +
                                        "   (A.DaThanhToan IS NULL OR A.DaThanhToan = 0)  AND  \n" +
                                        "   DATEDIFF(DAY, GETDATE(),  DATEADD(Day, A.SoNgayThanhToan, A.Ngay)) <= {0} \n" +
                                       "ORDER BY A.Ngay DESC",
                                        _GioiHanSoNgay);
                }
                else if (chkForBan.Checked)
                {
                    cmd.CommandText = string.Format("SELECT A.So SoHoaDon, B.So SoPhieu, A.Ngay, E.Ten, E.CongNoPhaiThuMax SoDuNoToiDa,  \n" +
                                       "	A.SoTienNT PhaiThu, DATEADD(Day, A.SoNgayThanhToan, A.Ngay) NgayPhaiThu, 0 PhaiTra, NULL NgayPhaiTra, \n" +
                                       "    DATEDIFF(DAY, GETDATE(),  DATEADD(Day, A.SoNgayThanhToan, A.Ngay)) SoNgayConLai \n" +
                                       "FROM ChungTu A INNER JOIN ChungTu B ON A.ChungTuID = B.ChungTuGocID \n" +
                                       "										AND @ThoiDiemDau <= A.Ngay AND A.Ngay <= @ThoiDiemCuoi AND A.GhiSo = 1  \n" +
                                       "	INNER JOIN KhoanMucChungTu C ON C.ChungTuID = B.ChungTuID \n" +
                                       "	INNER JOIN LoaiChungTu D ON D.ID = A.LoaiChungTuID AND D.Ma IN ('HoaDonBanHang') \n" +
                                       "	INNER JOIN DoiTuongNghiepVu E ON E.ID = A.DoiTuongNghiepVu1ID \n" +
                                       "WHERE A.SoNgayThanhToan IS NOT NULL AND A.SoNgayThanhToan > 0 AND \n" +
                                        "   (A.DaThanhToan IS NULL OR A.DaThanhToan = 0)  AND  \n" +
                                       "   	DATEDIFF(DAY, GETDATE(),  DATEADD(Day, A.SoNgayThanhToan, A.Ngay)) <= {0}  \n" +
                                       "ORDER BY A.Ngay DESC",
                                        _GioiHanSoNgay);
                }
                else if (chkForMua.Checked)
                {
                    cmd.CommandText =   string.Format("SELECT A.So SoHoaDon, B.So SoPhieu, A.Ngay, E.Ten, E.CongNoPhaiThuMax SoDuNoToiDa,  \n" +
                                        "	0 PhaiThu, NULL NgayPhaiThu, A.SoTienNT PhaiTra, DATEADD(Day, A.SoNgayThanhToan, A.Ngay) NgayPhaiTra,  \n" +
                                        "    DATEDIFF(DAY, GETDATE(),  DATEADD(Day, A.SoNgayThanhToan, A.Ngay)) SoNgayConLai \n" +
                                        "FROM ChungTu A INNER JOIN ChungTu B ON A.ChungTuID = B.ChungTuGocID \n" +
                                        "										AND @ThoiDiemDau <= A.Ngay AND A.Ngay <= @ThoiDiemCuoi AND A.GhiSo = 1  \n" +
                                        "	INNER JOIN KhoanMucChungTu C ON C.ChungTuID = B.ChungTuID \n" +
                                        "	INNER JOIN LoaiChungTu D ON D.ID = A.LoaiChungTuID AND D.Ma IN ('HoaDonMuaHang') \n" +
                                        "	INNER JOIN DoiTuongNghiepVu E ON E.ID = A.DoiTuongNghiepVu1ID \n" +
                                        "WHERE A.SoNgayThanhToan IS NOT NULL AND A.SoNgayThanhToan > 0 AND \n" +
                                        "   (A.DaThanhToan IS NULL OR A.DaThanhToan = 0)  AND  \n" +
                                        "   DATEDIFF(DAY, GETDATE(),  DATEADD(Day, A.SoNgayThanhToan, A.Ngay)) <= {0} \n" +
                                       "ORDER BY A.Ngay DESC",
                                        _GioiHanSoNgay);
                }

                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("ThoiDiemDau", dtNgayBatDau.DateTime);
                cmd.Parameters.AddWithValue("ThoiDiemCuoi", dtNgayKetThuc.DateTime);

                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(_DsSource.BangDanhGiaThanhToan);

                FormatNumberInGridView(gridView);
            }
            catch (Exception ex)
            {
                BasicForms.FrmExceptionNotifier.ShowAndLog(ex);
            }
            finally
            {
                con.Close();
                con = null;
                BasicForms.FrmProgressBar.FinishShowProgress();
            }
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            PrintWithHeader(false, gridControl, true, true, "Bảng đánh giá tinh hình thanh toán công nợ các đối tác", "");
        }
    }
}
